package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-04 18:46
 * @ Description :
 */
public class RomanDigit
{
    public static void main(String[] args)
    {
        String s = "MCMXCIV";
        int i = romanToInt(s);
        System.out.println("i = " + i);

    }

    public static int romanToInt(String s)
    {
        //IV IX XL XC CD CM
        HashMap<String, Integer> map = new HashMap<>();
        map.put("I", 1);
        map.put("V", 5);
        map.put("X", 10);
        map.put("L", 50);
        map.put("C", 100);
        map.put("D", 500);
        map.put("M", 1000);
        map.put("IV", 4);
        map.put("IX", 9);
        map.put("XL", 40);
        map.put("XC", 90);
        map.put("CD", 400);
        map.put("CM", 900);

        List<String> split = new ArrayList<>();
        for (int i = 0; i < s.length(); i++)
        {
            if (matches(s.charAt(i)))
            {
                if ((i + 1) <= s.length() - 1)
                {
                    String temp = s.charAt(i) + "" + s.charAt(i + 1) ;

                    if (map.containsKey(temp))
                    {
                        split.add(temp);
                        i++;
                        continue;
                    }
                }
            }
            split.add(s.charAt(i) + "");
        }

        int res = 0;
        for (String item : split)
        {
            res += map.get(item);
        }
        return res;
    }

    private static boolean matches(char ch)
    {
        return ch == 'I' || ch == 'X' || ch == 'C';
    }
}
